The JSON_SERIALIZE function takes a HASH, LIST, or structure variable and converts it into a JSON (JavaScript Object Notation) string.
This routine is written in the IDL language. Its source code can be found in the file json_serialize.pro in the lib subdirectory of the IDL distribution.
Tip: The JSON_SERIALIZE converts a LIST or HASH into raw JSON with no "pretty" printing. If you want to output prettified JSON (with appropriate indenting and line breaks), you can simply bypass JSON_SERIALIZE and print out your LIST or HASH using Implied Print.
mylist = LIST(!TRUE, !NULL, 42, 3.14, "Hello")
json = JSON_SERIALIZE(mylist)
PRINT, json
IDL prints:
[true,null,42,3.14,"Hello"]
myhash = HASH("Planet", "Jupiter", "Index", 5, "Mass", 1.9d27, "Units", "kg")
json = JSON_SERIALIZE(myhash)
PRINT, json
IDL prints:
{"Planet":"Jupiter","Index":5,"Units":"kg","Mass":1.9e27}
struct = {PLANET: "Jupiter", INDEX: 5, MASS: 1.9d27, UNITS: "kg"}
json = JSON_SERIALIZE(struct)
PRINT, json
IDL prints:
{"PLANET":"Jupiter","INDEX":5,"MASS":1.9e27,"UNITS":"kg"}
Result = JSON_SERIALIZE(Value, /LOWERCASE)
The result is a string containing the JSON string. If the input value is a LIST, then the JSON string will be encoded as a JSON array, with square brackets and comma-separated values. If the input value is a HASH, then the JSON string will be encoded as a JSON object, with curly braces and key-value pairs.
When converting IDL variables, the following rules are used:
Note: Since the HASH stores its key-value pairs in an arbitrary order, the key-value pairs in the resulting JSON string may be in a different order than the order in which the keys were added to the hash.
Value must be a HASH, LIST, or structure variable.
By default, when serializing an IDL structure, all of the structure tag names are in uppercase within the resulting JSON string. Set the LOWERCASE keyword to use lowercase for the structure tag names.
|
8.2 |
Introduced |
| 8.4 |
All byte values are now encoded as integers (instead of true/false). Added LOWERCASE keyword. Boolean variables are encoded as true/false. |
JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and for machines to parse and generate. JSON was designed as an alternative to XML, and is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. Further details can be found at http://www.json.org.